package com.heatup.apt.api.controller;

import com.heatup.apt.api.service.CourseListService;
import com.heatup.apt.common.constants.ErrorCodeConstants;
import com.heatup.apt.common.util.JsonTools;
import com.heatup.apt.common.util.RequestTools;
import com.heatup.apt.common.util.ResponseTools;
import com.heatup.apt.model.api.ApiResultModel;
import com.heatup.apt.model.user.UserInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 课程列表
 * <P>File name : TeamCourseController.java </P>
 * <P>Author : fangxiaowen </P>
 * <P>Date : 2016年8月23日 </P>
 */
@RequestMapping(value = "/course/list")
@Controller
public class CourseListController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(CourseListController.class);
    @Resource
    private CourseListService courseListService;


    /**
     * 团体课程list
     * TeamCourseController.team_course_list()<BR>
     * 
     * <P>Date : 2016年8月23日 </P>
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/team_course_list", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public String team_course_list(HttpServletRequest request) {
        ApiResultModel<Map> apiResultModel = new ApiResultModel<>(isLogined());
        String callback = RequestTools.RequestString(request, "callback", "");
        String curdate = RequestTools.RequestString(request, "curdate", "");
        Integer pageNo = RequestTools.RequestInt(request, "pageNo", 0);
        Integer pageSize = RequestTools.RequestInt(request, "pageSize", 10);
        Integer storeId = RequestTools.RequestInt(request, "storeId", 0);
        String longitudeLatitude = RequestTools.RequestString(request, "longitudeLatitude", "");
        UserInfo userInfo = this.getUserInfo();
        if (!isLogined()) {
            // 添加非空校验 防止 NullPointerException
            apiResultModel.setErrorCode(ErrorCodeConstants.Code.USER_NOT_LOGIN.getCode());
            apiResultModel.setMsg(ErrorCodeConstants.Code.USER_NOT_LOGIN.getMessage());
        } else {
            Map<String, Object> paramMap = new HashMap<>();
            paramMap.put("curdate", curdate);
            paramMap.put("pageNo", pageNo);
            paramMap.put("pageSize", pageSize);
            paramMap.put("storeId", storeId);
            paramMap.put("longitudeLatitude", longitudeLatitude);
            paramMap.put("userLevel", userInfo.getLevel() != null ? userInfo.getLevel() : 1);

            paramMap.put("userInfo", userInfo);
            logger.info("team_course_list param is : " + paramMap);
            try {
                apiResultModel.setData(courseListService.getTeamCourseList(paramMap));

            } catch (Exception e) {
                e.printStackTrace();
                apiResultModel.setRet(ApiResultModel.RET_ERROR);
                apiResultModel.setErrorCode(ErrorCodeConstants.Code.TEAM_ERROR_EXCEPTION.getCode());
                apiResultModel.setMsg(ErrorCodeConstants.Code.TEAM_ERROR_EXCEPTION.getMessage());
                //logger.error(ErrorCodeConstants.Code.TEAM_ERROR_EXCEPTION.getMessage() + e.toString() + e.getCause());
                logger.error("team_course_list 团课列表API异常：", e);
            }
        }
        return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(apiResultModel), callback);
    }

    /**
     * 私教课程list
     * CourseListController.pt_course_list()<BR>
     * 
     * <P>Date : 2016年8月23日 </P>
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/pt_course_list", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public String pt_course_list(HttpServletRequest request) {
        String callback = RequestTools.RequestString(request, "callback", "");
        Integer pageNo = RequestTools.RequestInt(request, "pageNo", 0);
        Integer pageSize = RequestTools.RequestInt(request, "pageSize", 10);
        Integer coachId = RequestTools.RequestInt(request, "coachId", 0);

        logger.info("get pt_course_list pageNo : " + pageNo + ", pageSize:" + pageSize);
        ApiResultModel<Map> apiResultModel = new ApiResultModel<>(isLogined());
        UserInfo userInfo = this.getUserInfo();
        if (userInfo == null) {
            // 添加非空校验 防止 NullPointerException
            apiResultModel.setErrorCode(ErrorCodeConstants.Code.USER_NOT_LOGIN.getCode());
            apiResultModel.setMsg(ErrorCodeConstants.Code.USER_NOT_LOGIN.getMessage());
        } else {
            try {
                apiResultModel.setData(courseListService.getPTCourseList(pageNo, pageSize, coachId, getUserId()));
            } catch (Exception e) {
                e.printStackTrace();
                //logger.error(ErrorCodeConstants.Code.SECRET_ERROR_EXCEPTION.getMessage() + e.toString() + e.getCause(),e);
                apiResultModel.setRet(ApiResultModel.RET_ERROR);
                apiResultModel.setErrorCode(ErrorCodeConstants.Code.SECRET_ERROR_EXCEPTION.getCode());
                apiResultModel.setMsg(ErrorCodeConstants.Code.SECRET_ERROR_EXCEPTION.getMessage() + e.toString() + e.getCause());
                logger.error("pt_course_list 私教列表API异常：", e +" 根原因 " + e.getCause());
            }
        }
        return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(apiResultModel), callback);
    }

    /**
     * 檬营列表分页
     * CourseListController.camp_course_list()<BR>
     * 
     * <P>Date : 2016年8月30日 </P>
     *
     * @param pageIndex         页码
     * @param pageSize          页面大小
     * @param longitudeLatitude 用户当前位置经纬度
     * @param callback          回调
     * @return
     */
    @RequestMapping(value = "/camp_course_list", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public String camp_course_list(
            @RequestParam Integer pageIndex,
            @RequestParam Integer pageSize,
            String longitudeLatitude,
            String callback) {
        if (pageIndex > 0)
            --pageIndex;
        if (pageSize <= 0)
            pageSize = 10;
        ApiResultModel<Map> apiResultModel = new ApiResultModel<>(isLogined());
        UserInfo userInfo = this.getUserInfo();
        if (userInfo == null) {
            // 添加非空校验 防止 NullPointerException
            apiResultModel.setErrorCode(ErrorCodeConstants.Code.USER_NOT_LOGIN.getCode());
            apiResultModel.setMsg(ErrorCodeConstants.Code.USER_NOT_LOGIN.getMessage());
        } else {
            try {
                apiResultModel.setData(courseListService.getCampCourseList(pageIndex, pageSize, longitudeLatitude, userInfo));
                apiResultModel.setRet(ApiResultModel.RET_SUCCESS);
            } catch (Exception e) {
                e.printStackTrace();
                logger.error(ErrorCodeConstants.Code.CAMP_ERROR_EXCEPTION.getMessage() + e.toString() + e.getCause());
                apiResultModel.setErrorCode(ErrorCodeConstants.Code.CAMP_ERROR_EXCEPTION.getCode());
                apiResultModel.setMsg(ErrorCodeConstants.Code.CAMP_ERROR_EXCEPTION.getMessage() + e.toString() + e.getCause());
                apiResultModel.setRet(ApiResultModel.RET_ERROR);
                logger.error("camp_course_list 檬营列表API异常：", e +" 根原因 " + e.getCause());
            }
        }
        return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(apiResultModel), callback);
    }

    /**
     * 自助健身列表
     *
     * @param pageIndex         页码
     * @param pageSize          页面大小
     * @param longitudeLatitude 经纬度
     * @param callback          回调
     * @return
     */
    @RequestMapping(value = "/self_fitness_list", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public String self_fitness_list(@RequestParam(required = false) Integer pageIndex,
                                    @RequestParam(required = false) Integer pageSize, String longitudeLatitude, String callback) {
        if (pageIndex == null)
            pageIndex = 1;
        if (pageSize == null)
            pageSize = 10;
        if (pageIndex > 0)
            --pageIndex;
        if (pageSize <= 0)
            pageSize = 10;
        ApiResultModel<Map> apiResultModel = new ApiResultModel<>(isLogined());
        UserInfo userInfo = this.getUserInfo();
        if (userInfo == null) {
            // 添加非空校验 防止 NullPointerException
            apiResultModel.setErrorCode(ErrorCodeConstants.Code.USER_NOT_LOGIN.getCode());
            apiResultModel.setMsg(ErrorCodeConstants.Code.USER_NOT_LOGIN.getMessage());
        } else {
            try {
                apiResultModel.setData(courseListService.getSelfFitnessList(pageIndex, pageSize, longitudeLatitude, userInfo.getId()));
            } catch (Exception e) {
                e.printStackTrace();
                logger.error(
                        ErrorCodeConstants.Code.SELFFITNISS_ERROR_EXCEPTION.getMessage() + e.toString() + e.getCause());
                apiResultModel.setRet(ApiResultModel.RET_ERROR);
                apiResultModel.setErrorCode(ErrorCodeConstants.Code.SELFFITNISS_ERROR_EXCEPTION.getCode());
                apiResultModel.setMsg(
                        ErrorCodeConstants.Code.SELFFITNISS_ERROR_EXCEPTION.getMessage() + e.toString() + e.getCause());
                logger.error("camp_course_list 自助健身列表API异常：", e);
            }
        }
        return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(apiResultModel), callback);
    }
}